package com.to3ks.excelservice.utils;


import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;

import com.to3ks.excelservice.entity.XlsSwryData;
import com.to3ks.excelservice.listener.ExcelDataListener;
import com.to3ks.excelservice.service.XlsSwryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class ImportExcelUtils {


    /**
     * 07版本excel读数据量大于1千行，内部采用回调方法.
     *
     * @throws IOException 简单抛出异常，真实环境需要catch异常,同时在finally中关闭流
     */

    public static void saxReadListStringV2007A(HttpServletRequest httpServletRequest, XlsSwryService xlsSwryService) throws IOException {

        String filenameWithPath= (String)httpServletRequest.getAttribute("xlsFile=");

        httpServletRequest.setAttribute("total_invoke",0);

        //log.info("*********要处理的文件流是:"+filenameWithPath);

        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filenameWithPath));
        AnalysisEventListener excelListener = new ExcelDataListener(xlsSwryService);
        EasyExcelFactory.readBySax(bufferedInputStream, new Sheet(1, 1, XlsSwryData.class), excelListener);

    }

}
